package generic.swing.tabs;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;

/**
 * Adapted from http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java
 *
 *
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class TabButton extends JButton {
    private static final Color normalColor = Color.BLACK;
    private static final Color hoverColor = Color.BLUE;
    private Color color = normalColor;

    public TabButton() {
        Dimension size = new Dimension(17, 17);

        setPreferredSize(size);
        setToolTipText("Close tab");

        setContentAreaFilled(false);
        setFocusable(false);



        addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                color = hoverColor;
                repaint();
            }

            public void mouseExited(MouseEvent e) {
                color = normalColor;
                repaint();
            }
        });

        setRolloverEnabled(true);

    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        //shift the image for pressed buttons
        if (getModel().isPressed()) {
            g2.translate(1, 1);
        }
        g2.setStroke(new BasicStroke(2));
        g2.setColor(color);
        int delta = 6;
        g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
        g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
        g2.dispose();
    }


}
